//
//  YDActivityManager.m
//  IngCard
//
//  Created by kf on 2017/9/8.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDActivityManager.h"
#import "YDAPI.h"
#import "YDHttpClient.h"
#import "YDActivity.h"
#import "YDAccountManager.h"
#import "NSDictionary+Format.h"

@interface YDActivityManager()
@property(nonatomic, strong) NSString *lastAid;
@end

@implementation YDActivityManager
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    static YDActivityManager *instance = nil;
    dispatch_once(&onceToken,^{
        instance = [[super allocWithZone:NULL] init];
        instance.hasMore = YES;
    });
    return instance;
}

+ (id)allocWithZone:(struct _NSZone *)zone{
    return [self sharedInstance];
}

//NSArray<NSDictionary<date,NSArray<NSString *>>>
-(void) sync:(NSMutableDictionary<NSString*,NSArray<NSString *>*> *) info
  OnComplete:(SyncComplete) onComplete;
{
    [HTTP POST:API.activity_sync parameters:^(NSMutableDictionary *parameters) {
        
        NSMutableDictionary* temp = [NSMutableDictionary dictionary];
        [temp safeSetObject:@[@"333",@"444",@"555"] forKey:@"1505380452"];
        
        [temp safeSetObject:@[@"33",@"44",@"55"] forKey:@"1505380352"];
        
        [temp safeSetObject:@[@"3"] forKey:@"1505370352"];
        
        [parameters safeSetObject:[temp serializeJson] forKey:@"c_cid"];
        [parameters safeSetObject:ACCOUNT.account.uid forKey:@"uid"];
        [parameters safeSetObject:ACCOUNT.account.login_token forKey:@"login_token"];
        
    } onComplete:^(BOOL success, NSDictionary *response, NSString *msg) {
    
        NSArray *activities = NSArrayMake(response[@"activities"]);
        NSMutableArray *syncActivities = [NSMutableArray array];
        NSMutableArray *syncContacts = [NSMutableArray array];
        
        for (int i = 0; i < activities.count ;i++)
        {
            NSDictionary *activityDic = NSDictionaryMake(activities[i]);
            YDActivity *activity = [YDActivity serializeWithSyncJson:activityDic];
            
            [syncActivities addObject:activity];
            [syncContacts addObjectsFromArray:activity.contacts];
        }
        
        self.syncActivities = syncActivities;
        self.syncContacts = syncContacts;
        self.beAdded = NO;
        
        BlockGoGo(onComplete,success,msg,syncActivities, syncContacts);
    }];
}

- (void) getMoreOnComplete:(void (^)(BOOL success, NSString* msg)) onComplete
{
    [HTTP POST:API.activity_list parameters:^(NSMutableDictionary *parameters) {
        [parameters safeSetObject:ACCOUNT.account.uid forKey:@"uid"];
        [parameters safeSetObject:ACCOUNT.account.login_token forKey:@"login_token"];
        
        if (self.lastAid)
        {
            [parameters safeSetObject:self.lastAid forKey:@"aid"];
        }
    } onComplete:^(BOOL success, NSDictionary *response, NSString *msg) {
        if(success)
        {
            NSArray *activities = NSArrayMake(response[@"activites"]);
            (activities.count <5) ? (self.hasMore = NO) : (self.hasMore = YES);
            
            for (int i = 0; i < activities.count ;i++)
            {
                NSDictionary *activityDic = NSDictionaryMake(activities[i]);
                [self.activities addObject:[YDActivity serializeWithJson:activityDic]];
            }
            
            self.lastAid = NSStringMake(response[@"last_aid"]);
        }
        
        BlockGoGo(onComplete,success,msg);
    }];
}

-(void) modify:(NSString *)activityID
          desc:(NSString *) desc
    OnComplete:(void (^)(BOOL success, NSString* msg)) onComplete
{
    [HTTP POST:API.activity_modify parameters:^(NSMutableDictionary *parameters) {
        [parameters safeSetObject:ACCOUNT.account.uid forKey:@"uid"];
        [parameters safeSetObject:ACCOUNT.account.login_token forKey:@"login_token"];
        [parameters safeSetObject:activityID forKey:@"aid"];
        [parameters safeSetObject:desc forKey:@"desc"];
    } onComplete:^(BOOL success, NSDictionary *response, NSString *msg) {
        
    }];
}

#pragma mark - lazy loading
-(NSMutableArray<YDActivity *> *) activities
{
    if (!_activities)
    {
        _activities = [NSMutableArray array];
    }
    return _activities;
}
@end
